package com.stoneapple.timesheet.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.stoneapple.timesheet.entity.Project;

public class ProjectDao {
	private static ProjectDao instance;

	private ProjectDao() {
	}

	public static ProjectDao getInstance() {
		if (instance == null) {
			instance = new ProjectDao();
		}
		return instance;
	}

	
	@SuppressWarnings("unchecked")
	public List<Project> getProjectList() {
		EntityManager em = EMF.get().createEntityManager();
		Query query = em.createQuery("SELECT * FROM Project  ");
		List<Project> project = query.getResultList();
		return project;
	}

	@SuppressWarnings("unchecked")
	public List<Project> getProjectListByUserId(String userId) {
		EntityManager em = EMF.get().createEntityManager();
		Query query = em.createQuery("SELECT P FROM Project P WHERE author = :userId");
		query.setParameter("userId", userId);
		List<Project> project = query.getResultList();
		return project;
	}

	public void save (String userId,String projectcode, String projectname,String detail,String employeecode,String descrition ) {
		synchronized (this) {
			EntityManager em = EMF.get().createEntityManager();
			Project project=new Project(userId, projectcode, projectname, detail,employeecode,descrition);
			try {
				em.persist(project);
			} finally {
				em.close();
			}
		}
	}

	

}
